"""
配置管理模块
"""

from pydantic_settings import BaseSettings
from functools import lru_cache


class Settings(BaseSettings):
    """应用配置"""

    # 应用信息
    APP_NAME: str = "智面"
    APP_VERSION: str = "1.0.0"
    DEBUG: bool = False

    # 数据库配置
    DATABASE_HOST: str = "localhost"
    DATABASE_PORT: int = 5432
    DATABASE_USER: str = "zhimian"
    DATABASE_PASSWORD: str = "zhimian_password"
    DATABASE_NAME: str = "zhimian"
    # Redis 配置
    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = "zhimian_password"

    # JWT 配置
    SECRET_KEY: str
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30

    # LLM 配置
    LLM_API_KEY: str = "sk-dummy"  # 使用 Ollama 时可以是任意值
    LLM_BASE_URL: str = "https://api.openai.com/v1"  # 默认 OpenAI API
    LLM_MODEL: str = "gpt-4"  # 默认模型
    LLM_TEMPERATURE: float = 0.7  # 默认温度

    # Embedding 配置
    EMBEDDING_PROVIDER: str = "ollama"  # 可选: "openai" 或 "ollama"
    EMBEDDING_MODEL: str = (
        "nomic-embed-text"  # OpenAI: text-embedding-ada-002, Ollama: nomic-embed-text
    )
    EMBEDDING_API_KEY: str = "sk-dummy"  # 使用 Ollama 时可以是任意值
    EMBEDDING_BASE_URL: str = "https://api.openai.com/v1"  # 默认 OpenAI API
    # Ollama API 地址
    # Docker 环境使用: http://host.docker.internal:11434
    # 本地开发使用: http://localhost:11434
    OLLAMA_BASE_URL: str = "http://host.docker.internal:11434"

    class Config:
        env_file = ".env"
        case_sensitive = True

    @property
    def get_database_url(self) -> str:
        return f"postgresql://{self.DATABASE_USER}:{self.DATABASE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_NAME}"


@lru_cache()
def get_settings() -> Settings:
    """获取配置实例（单例模式）"""
    return Settings()
